import routeSettings from "@/config/route"
import { createRouter, type RouteRecordRaw } from "vue-router"
import { flatMultiLevelRoutes, history } from "./helper"

const Layouts = () => import("@/layouts/index.vue")

type NewRouteRecordRaw = RouteRecordRaw & { isFull?: boolean; isHidden?: boolean }
/**
 * 常驻路由
 * 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置 Name 属性
 */
export const constantRoutes: Array<NewRouteRecordRaw> = [
  {
    path: "/redirect",
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/views/error-page/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/",
    component: Layouts,
    redirect: "/dashboard",
    children: [
      {
        path: "",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          svgIcon: "icon-a-mianxingshouye",
          affix: true
        }
      }
    ]
  },
  // AI·智能体
  {
    path: "/",
    component: Layouts,
    redirect: "/model",
    meta: {
      title: "AI·智能体",
      svgIcon: "icon-a-mianxingAIyunmoxing"
    },
    children: [
      {
        path: "model",
        component: () => import("@/views/cloudModel/index.vue"),
        name: "CloudModel",
        meta: {
          isHidden: true,
          title: "云模型",
          svgIcon: "icon-yunmoxing"
        }
      },
      {
        path: "intelligent",
        name: "IntelligentService",
        component: () => import("@/views/intelligentService/index.vue"),
        meta: {
          title: "云模型",
          hidden: true
        }
      },
      {
        path: "knowledge",
        component: () => import("@/views/knowledgeBase/index.vue"),
        name: "KnowledgeBase",
        meta: {
          title: "知识库",
          svgIcon: "icon-zhishiku"
        }
      },
      {
        path: "/knowledge/details",
        name: "KnowledgeDetails",
        component: () => import("@/views/knowledgeBase/components/knowledgeDetails.vue"),
        meta: {
          title: "知识库",
          hidden: true
        }
      },
      // 智能体聊天记录
      {
        path: "setting/token",
        component: () => import("@/views/setting/token/index.vue"),
        name: "Token",
        meta: {
          title: "智能体聊天记录",
          svgIcon: "icon-liaotianjilu"
        }
      }
    ]
  },
  // AI-图片
  {
    path: "/aicoverpicture",
    component: Layouts,
    redirect: "/aicoverpicture",
    children: [
      {
        path: "",
        component: () => import("@/views/aiCoverPicture/index.vue"),
        name: "aiCoverPicture",
        meta: {
          title: "AI·生图",
          svgIcon: "icon-a-mianxingAIshengtu"
        }
      }
    ]
  },

  // 语音
  {
    path: "/speech",
    component: Layouts,
    redirect: "/list",
    meta: {
      title: "语音",
      svgIcon: "icon-a-mianxingyuyin"
    },
    children: [
      // 语音列表
      {
        path: "list",
        component: () => import("@/views/speech/speechModelList/index.vue"),
        name: "SpeechModelList",
        meta: {
          title: "语音列表",
          hidden: false,
          svgIcon: "icon-yuyinliebiao"
        }
      },
      // 语音模型
      {
        path: "model",
        component: () => import("@/views/speech/speechModel/index.vue"),
        name: "SpeechModel",
        meta: {
          title: "语音模型",
          hidden: false,
          svgIcon: "icon-yuyinmoxing"
        }
      }
    ]
  },
  // 超级智库
  {
    path: "/super-idea",
    component: Layouts,
    redirect: "/super-idea",
    children: [
      {
        path: "",
        component: () => import("@/views/super-idea/index.vue"),
        name: "suerIdea",
        meta: {
          isFull: true,
          title: "超级智库",
          svgIcon: "icon-chaojizhiku"
        }
      }
    ]
  },
  //支付中台
  {
    path: "/payment",
    component: Layouts,
    redirect: "/orders/index",
    meta: {
      title: "支付中台",
      svgIcon: "icon-zhifuzhongtai"
    },
    children: [
      {
        path: "",
        component: () => import("@/views/paymentCenter/orders/index.vue"),
        name: "Orders",
        meta: {
          title: "订单",
          hidden: false,
          svgIcon: "icon-dingdan"
        }
      },
      {
        path: "product",
        component: () => import("@/views/paymentCenter/product/index.vue"),
        name: "Product",
        meta: {
          title: "产品",
          svgIcon: "icon-chanpin1"
        }
      },
      {
        path: "qrCode",
        component: () => import("@/views/paymentCenter/qrCode/index.vue"),
        name: "QrCode",
        meta: {
          title: "二维码",
          svgIcon: "icon-chanpin1"
        }
      }
    ]
  },
  // 管理员
  {
    path: "/administrators",
    component: Layouts,
    redirect: "/administrators",
    children: [
      {
        path: "",
        component: () => import("@/views/administrators/index.vue"),
        name: "Administrators",
        meta: {
          title: "管理员",
          svgIcon: "icon-a-mianxingguanliyuan"
        }
      }
    ]
  },
  // 数字人
  {
    path: "/digital",
    component: Layouts,
    redirect: "/digital",
    // meta: {
    //   // isFull: true,
    //   title: "数字人",
    //   svgIcon: "icon-shuziren"
    // },
    // // TODO: 测试，后续需要删除
    children: [
      {
        path: "",
        component: () => import("@/views/digitalPeople/index.vue"),
        name: "Digital",
        meta: {
          title: "数字人",
          svgIcon: "icon-shuziren"
        }
      }
      /*{
        path: "robot",
        component: () => import("@/views/digitalPeople/robot.vue"),
        name: "Robot",
        meta: {
          title: "数字人2",
          isFull: true,
          svgIcon: "icon-shuziren"
        }
      },
      */
    ]
  },
  // 设置
  {
    path: "/setting",
    component: Layouts,
    redirect: "/setting/log",
    meta: {
      title: "更多",
      svgIcon: "icon-a-mianxinggengduo"
    },
    children: [
      // 日志
      {
        path: "log",
        component: () => import("@/views/setting/log/index.vue"),
        name: "Log",
        meta: {
          title: "个人设置",
          hidden: true
        }
      },
      // 标签
      {
        path: "tags",
        component: () => import("@/views/setting/tags/index.vue"),
        name: "Tags",
        meta: {
          title: "标签",
          hidden: false,
          svgIcon: "icon-biaoqian"
        }
      },
      // 中台key
      // {
      //   path: "middlegroundKey",
      //   component: () => import("@/views/middlegroundKey/index.vue"),
      //   name: "Key",
      //   meta: {
      //     title: "Key",
      //     hidden: false
      //   }
      // },

      // sdk文档
      // sdk文档
      {
        path: "sdk",
        component: () => import("@/views/sdk/index.vue"),
        name: "Sdk",
        meta: {
          title: "sdk文档",
          hidden: false,
          svgIcon: "icon-SDKwendang"
        }
      },
      {
        path: "sdk/:doc",
        component: () => import("@/views/sdk/index.vue"),
        meta: {
          title: "sdk文档",
          hidden: true
        }
      }
    ]
  },
  //会员中心
  {
    path: "/member",
    component: Layouts,
    redirect: "/member",
    meta: {
      hidden: true
    },
    children: [
      {
        path: "",
        component: () => import("@/views/member/index.vue"),
        name: "Member",
        meta: {
          title: "会员中心",
          svgIcon: "member"
        }
      }
    ]
  }
]

/**
 * 动态路由
 * 用来放置有权限 (Roles 属性) 的路由
 * 必须带有 Name 属性
 */
export const asyncRoutes: RouteRecordRaw[] = [
  // {
  //   path: "/permission",
  //   component: Layouts,
  //   redirect: "/permission/page",
  //   name: "Permission",
  //   meta: {
  //     title: "权限管理",
  //     svgIcon: "lock",
  //     roles: ["admin", "editor"], // 可以在根路由中设置角色
  //     alwaysShow: true // 将始终显示根菜单
  //   },
  //   children: [
  //     {
  //       path: "page",
  //       component: () => import("@/views/permission/page.vue"),
  //       name: "PagePermission",
  //       meta: {
  //         title: "页面权限",
  //         roles: ["admin"] // 或者在子导航中设置角色
  //       }
  //     },
  //     {
  //       path: "directive",
  //       component: () => import("@/views/permission/directive.vue"),
  //       name: "DirectivePermission",
  //       meta: {
  //         title: "指令权限" // 如果未设置角色，则表示：该页面不需要权限，但会继承根路由的角色
  //       }
  //     }
  //   ]
  // },
  //管理员
  // {
  //   path: "/administrators",
  //   component: Layouts,
  //   redirect: "/administrators",
  //   meta: {
  //     roles: ["admin"] // 或者在子导航中设置角色
  //   },
  //   children: [
  //     {
  //       path: "index",
  //       component: () => import("@/views/administrators/index.vue"),
  //       name: "Administrators",
  //       meta: {
  //         title: "管理员",
  //         svgIcon: "icon-a-mianxingguanliyuan",
  //         roles: ["admin"] // 或者在子导航中设置角色

  //       }
  //     },
  //   ]
  // },
  {
    path: "/:pathMatch(.*)*", // Must put the 'ErrorPage' route at the end, 必须将 'ErrorPage' 路由放在最后
    redirect: "/404",
    name: "ErrorPage",
    meta: {
      hidden: true
    }
  }
]

const router = createRouter({
  history,
  routes: routeSettings.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
  try {
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    window.location.reload()
  }
}

export default router
